﻿using AutoCommunication.Base;
using AutoCommunication.Enums;
using AutoCommunication.Helper;
using System;
using System.Drawing;
using System.Threading.Tasks;

#if NET461_OR_GREATER || NET5_0_OR_GREATER

using System.Windows.Forms;

namespace AutoCommunication
{
    public partial class Form_ShowDevice : Form
    {
        private IDevice dev;

        public Form_ShowDevice(IDevice device)
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            this.Text = device.Name;
            listView_ResultInfo.Columns[1].Width = listView_ResultInfo.Width - listView_ResultInfo.Columns[0].Width - 50;
            dev = device;
            this.propertyGrid1.SelectedObject = dev;
            // var res = dev.StartAsync();
        }

        private Form form;

        private void button1_Click(object sender, EventArgs e)
        {
            var res = dev.Start();
            if (res.IsSucceed)
            {
                var item = new ListViewItem(new string[] { DateTime.Now.ToString(), $"{dev.Name}初始化成功" }, 0);
                listView_ResultInfo.Items.Insert(0, item);
                form = new Form() { Text = dev.Name };
                form.FormClosed += Form_FormClosed;
                FlowLayoutPanel Panel = new FlowLayoutPanel();
                Panel.Dock = DockStyle.Fill;
                foreach (var varable in Auto.AllVarable)
                {
                    if (varable.Key.StartsWith(dev.Name.Norm()))
                    {
                        Label label = new Label();
                        label.AutoSize = true;
                        label.Margin = new Padding(1);
                        label.Text = varable.Key + ":";
                        label.BackColor = Color.LightBlue;
                        Panel.Controls.Add(label);
                        TextBox textBox = new TextBox();
                        textBox.Margin = new Padding(5, 1, 1, 10);
                        textBox.Width = 150;
                        textBox.Name = varable.Key;
                        textBox.AccessibleName = varable.Key;
                        textBox.MouseDoubleClick += (s, ee) =>
                        {
                            ChangeVarableValue changeVarableValue = new ChangeVarableValue((s as TextBox).Name);
                            changeVarableValue.ShowDialog();
                        };
                        Panel.Controls.Add(textBox);
                    }
                }
                form.Controls.Add(Panel);
                form.Show();
                form.Auto();
            }
            else
            {
                listView_ResultInfo.Items.Add(new ListViewItem(new string[] { DateTime.Now.ToString(), $"{dev.Name}初始化失败" }, 0));
                foreach (var item in res.ErrList)
                {
                    var item2 = new ListViewItem(new string[] { DateTime.Now.ToString(), $"{item}" }, 1);

                    listView_ResultInfo.Items.Insert(0, item2);
                }
            }
        }

        private void Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            button2_Click(null, null);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var res = dev.Stop();
            if (res.IsSucceed)
            {
                listView_ResultInfo.Items.Insert(0, new ListViewItem(new string[] { DateTime.Now.ToString(), $"{dev.Name}停止停止成功" }, 3));
            }
            else
            {
                listView_ResultInfo.Items.Insert(0, new ListViewItem(new string[] { DateTime.Now.ToString(), $"{dev.Name}停止失败" }, 0));
                foreach (var item in res.ErrList)
                {
                    var item2 = new ListViewItem(new string[] { DateTime.Now.ToString(), $"{item}" }, 1);

                    listView_ResultInfo.Items.Insert(1, item2);
                }
            }
        }

        private void button3_ClickAsync(object sender, EventArgs e)
        {
            var res = dev.Start(loger);
            textBox1.DataBindings.Clear();
            vScrollBar1.DataBindings.Clear();
            button4.DataBindings.Clear();
            label1.DataBindings.Clear();
            while (!Auto.AllVarable.ContainsKey("西门子PLC0_MB0"))
            {
                Task.Delay(1000).Wait();
            }
            var xx = Auto.AllVarable["西门子PLC0_MB0"];
            // Binding binding = new Binding("Text",xx,"Value",true);
            textBox1.DataBindings.Add("Text", xx, "Value");
            //textBox1.DataBindings.Add(binding);
            vScrollBar1.DataBindings.Add("Value", xx, "Value", true);
            button4.DataBindings.Add("Text", xx, "Value", true);
            label1.DataBindings.Add("Text", xx, "Value", true);
            //  Control c=new Control();
            // c.DataBindings.Add("Text",xx,"Value",true);
        }

        private void loger(string name, Exception ex, AlarmLevel alarmLevel)
        {
            for (int i = 4; i < listView_ResultInfo.Items.Count; i++)
            {
                listView_ResultInfo.Items.RemoveAt(i);
            }
            listView_ResultInfo.Items.Insert(0, new ListViewItem(new string[] { DateTime.Now.ToString(), $"{dev.Name} : {name}" }, 3));
        }

        private void button4_Click(object sender, EventArgs e)
        {
        }
    }
}

#endif